package com.dhms.vostok1.data.fault

import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.dhms.vostok1.data.BasePageQueryData
import com.dhms.vostok1.data.DEFAULT_PAGE_LIMIT
import com.dhms.vostok1.data.DEFAULT_PAGE_OFFSET
import com.dhms.vostok1.data.SecondaryBaseListData
import com.dhms.vostok1.data.abnormals.FaultState
import com.squareup.moshi.Json

/**
collect_point_id: 500020000533
cp_name: "泵驱动端"
ct_id: 30
ct_name: "设备类"
cu_id: 2
cu_name: "山东科大机电科技股份有限公司"
de_has_sensor: "yes"
de_id: 3
de_name: "1#渣浆泵"
dp_id: 2
dp_name: "设备管理部"
fa_alert_detail: ""
fa_extending: {}
fa_fault_type_id: 2000000
fa_handle_elapsed: 0
fa_id: 500020000004769
fa_last_occurred: "2022-06-29 16:58:01"
fa_max_value: "55"
fa_metric_no: "m.1756"
fa_min_value: "55"
fa_occurred: "2022-06-29 16:58:01"
fa_occurred_times: 1
fa_occurred_steps: "3.046,1,1656296085\n3.19,1,1656295955\n3.378,1,1656295890\n4.066,2,1656295825\n4.077,2,1656295760\n4.913,2,1656295695\n3.204,1,1656295630\n4.228,2,1656295565\n3.506,1,1656295500\n4.11,2,1656295435\n4.971,2,1656295370\n4.364,2,1656295305\n3.252,1,1656295240\n3.704,1,1656295175\n3.357,1,1656295110\n4.725,2,1656295045\n3.607,1,1656294980\n3.314,1,1656294915\n3.23,1,1656294850\n3.352,1,1656294785\n3.218,1,1656294720\n3.213,1,1656294655\n3.137,1,1656294590\n5.198,3,1656294525\n4.172,2,1656294460\n3.1,1,1656294395\n3.208,1,1656294330\n3.144,1,1656294265\n3.853,1,1656294200\n4.201,2,1656294135\n3.495,1,1656294070\n4.648,2,1656294005\n3.048,1,1656293940",
fa_source: 0
fa_state: "occurred"
fa_value: "55"
fa_vibfilename: ""
fl_background_color: "#c60202"
fl_level: "特别严重"
fl_no: 4
ft_type: "智能告警"
pa_analyse_supported: 0
pa_name: "速度均方根"
pa_threshold: 40
pa_thresholds: "10,20,30,40"
pa_thresholds_ext: ""
smart_diagnosis_fault_type_id: ""
 */
data class FaultListItemData(
    @Json(name = "collect_point_id")
    val collectPointId: Long?,
    @Json(name = "cp_name")
    val cpName: String?,
    @Json(name = "ct_id")
    val ctId: Int?,
    @Json(name = "ct_name")
    val ctName: String?,
    @Json(name = "cu_id")
    val cuId: Long?,
    @Json(name = "cu_name")
    val cuName: String?,
    @Json(name = "de_has_sensor")
    val deHasSensor: String?,
    @Json(name = "de_id")
    val deId: Long?,
    @Json(name = "de_name")
    val deName: String?,
    @Json(name = "dp_id")
    val dpId: Long?,
    @Json(name = "dp_name")
    val dpName: String?,
    @Json(name = "fa_alert_detail")
    val faAlertDetail: String?,
    @Json(name = "fa_fault_type_id")
    val faFaultTypeId: Int?,
    @Json(name = "fa_handle_elapsed")
    val faHandleElapsed: Int?,
    @Json(name = "fa_id")
    val faId: Long?,
    @Json(name = "fa_max_value")
    val faMaxValue: String?,
    @Json(name = "fa_metric_no")
    val faMetricNo: String?,
    @Json(name = "fa_min_value")
    val faMinValue: String?,
    @Json(name = "fa_occurred")
    val faOccurred: String?,
    @Json(name = "fa_occurred_times")
    val faOccurredTimes: Int?,
    @Json(name = "fa_source")
    val faSource: Int?,
    @Json(name = "fa_state")
    val faState: FaultState?,
    @Json(name = "fa_value")
    val faValue: String?,
    @Json(name = "fa_vibfilename")
    val faVibfilename: String?,
    @Json(name = "fl_background_color")
    val flBackgroundColor: String?,
    @Json(name = "fl_level")
    var flLevel: String?,
    @Json(name = "fl_no")
    val flNo: Int?,
    @Json(name = "ft_type")
    val ftType: String?,
    @Json(name = "pa_analyse_supported")
    val paAnalyseSupported: Int?,
    @Json(name = "pa_name")
    val paName: String?,
    @Json(name = "pa_threshold")
    val paThreshold: Float?,
    @Json(name = "pa_thresholds")
    val paThresholds: String?,
    @Json(name = "pa_thresholds_ext")
    val paThresholdsExt: String?,
    @Json(name = "smart_diagnosis_fault_type_id")
    val smartDiagnosisFaultTypeId: String?
)

data class FaultListQueryData(
    @Json(name = "de_id") var deviceId: Long? = null,
    @Json(name = "de_category_cu_id") var deviceCategoryCuId: Int? = null,
    @Json(name = "pa_usetypes") var paUseType: String? = "1,5",   // 设备告警
    @Json(name = "fa_states") val faStatus: String? = null,
    @Json(name = "fa_occurred_start") var occurredStart: String? = null,
    @Json(name = "fa_occurred_end") var occurredEnd: String? = null,
    @Json(name = "order_by") var orderBy: String? = "fl_no DESC,fa_occurred DESC",    //
    override var limit: Int = DEFAULT_PAGE_LIMIT,
    override var offset: Int = DEFAULT_PAGE_OFFSET,
) : BasePageQueryData

data class FaultDetail(
    @Json(name = "fa_id")
    val faId: Long?,
    @Json(name = "fl_level")
    val flLevel: String?,
    @Json(name = "de_name")
    val deName: String?,
    @Json(name = "collection_point_name")
    val cpName: String?,
    @Json(name = "fa_metric_no")
    val cpMetricNO: String?,
    @Json(name = "pa_name")
    val paName: String?,
    @Json(name = "fa_value")
    val faValue: String?,
    @Json(name = "fa_occurred")
    val faOccurred: String?,
    @Json(name = "fa_last_occurred")
    val faLastOccurred: String?,
    @Json(name = "fa_occurred_times")
    val faOccurredTimes: Int?,
    @Json(name = "fa_occurred_steps")
    val faOccurredSteps: String?,
    @Json(name = "fa_device_id")
    val deId: Long?,
    @Json(name = "pa_thresholds")
    val paThresholds: String?,
    @Json(name = "fa_fault_level_no")
    val flNo: Int?,
    @Json(name = "fa_extending")
    val faExtending: FaExtending?,
    @Json(name = "fa_state")
    val faState: FaultState?
)
typealias FaultListResData = SecondaryBaseListData<FaultListItemData>

@Entity(tableName = "fault_level")
data class FaultLevel(
    @Json(name = "background_color")
    @ColumnInfo(name = "background_color")
    val backgroundColor: String?,
    @NonNull val color: String?,
    @NonNull val name: String?,
    @PrimaryKey val no: Int,
)

typealias FaultLevelListData = List<FaultLevel>

data class FaultStatisticData(
    val level: Int?,
    @Json(name = "level_name") val levelName: String?,
    @Json(name = "fault_count") val faultCount: String?,
)

typealias FaultStatisticResData = List<FaultStatisticData>

data class FaExtending(
    @Json(name = "steps")
    val steps: List<ProcessFlowData>?
)

data class ProcessFlowData(
    @Json(name = "note")
    val note: String?,
    @Json(name = "state")
    val state: FaultState,
    @Json(name = "time")
    val time: String?,
    @Json(name = "user_id")
    val userId: Int?,
    @Json(name = "user_name")
    val userName: String?
)
